//@ts-ignore
import packageJSON from '../package.json';
import { ExecuteSceneScriptMethodOptions } from '../@types/packages/scene/@types/public';
import Path = require("path");
import * as fse from 'fs-extra';
import { readFileSync } from 'fs-extra';
import { PrefabParse } from './PrefabParse';
import { ParseResult } from './ParseResult';

/**
 * @en 
 * @zh 为扩展的主进程的注册方法
 */
export const methods: { [key: string]: (...any: any) => any } = {

  /**
   * @zh 收集assets目录下所有的预制件和场景 
   */
  async seek_miss() {

    let _mapDir = (path: string, fileList: Array<string>) => {
      let state = fse.statSync(path);
      if (state.isDirectory()) {
        let files = fse.readdirSync(path);
        for (let file of files) {
          _mapDir(Path.join(path, file), fileList);
        }
      }
      else {
        let ext = Path.extname(path);
        if (ext == '.prefab' || ext == '.scene') {
          fileList.push(path);
        }
      }
    }

    let rootPath = Path.join(Editor.Project.path, 'assets');
    let fileList: Array<string> = [];
    _mapDir(rootPath, fileList);

    //此时 fileList里已经存放着所有的scene和prefab的完整的URL了
    console.log('[seek miss]seek miss is working! Please Wait');
    let prefabParse: PrefabParse = new PrefabParse();
    for (let path of fileList) {
      await prefabParse.testPrefab(path);
    }
    //输出得到的结果
    let result: ParseResult = prefabParse.getResult();
    if (result._uuidMissedMap.size) {
      for (let ele of result._uuidMissedMap) {
        console.log("[seek miss]==================================================Split line==================================================");
        let prefabInfo = ele[1];
        prefabInfo.dump();

      }

      // Editor.Panel.open('listPanel');

    }
    else {
      console.log('[seek miss] Congratulations， all prefab and scene has no uuid miss!!');
    }




  },

  // _mapDir(path: string, fileList: Array<string>) {
  //   let state = fse.statSync(path);
  //   if (state.isDirectory()) {
  //     let files = fse.readdirSync(path);
  //     this._mapDir(files, fileList);
  //   }
  //   else {
  //     fileList.push(path);
  //   }
  // }





};

/**
 * @en Hooks triggered after extension loading is complete
 * @zh 扩展加载完成后触发的钩子
 */
export const load = function () { };

/**
 * @en Hooks triggered after extension uninstallation is complete
 * @zh 扩展卸载完成后触发的钩子
 */
export const unload = function () { };
